Centos 7 搭建Apache(Httpd 2.4) 文件服务器与Httpd文件上传

26 篇文章 0 订阅
7 篇文章 0 订阅

1、默认执行安装(更新时间:2020年7月1日20:39:28)

yum -y install httpd

2、启动、关闭、查看状态

# 启动
/bin/systemctl start httpd.service
# 关闭
/bin/systemctl stop httpd.service
# 查看状态
systemctl status httpd

 3、修改配置

3.1 用yum安装的一般默认安装/etc下面, 我的安装在/etc/httpd

3.2 用vi编辑配置文件

    vi /etc/httpd/conf/httpd.conf

    # 修改端口号
    Listen 8888

    # 解决跨域(也可以不加)
    LoadModule headers_module modules/mod_headers.so

    # 找到ServerName, 格式ip:端口号(列: 132.123.0.123:8888),如果有域名直接用域名
    ServerName www.xxxxx.cn:8888

    # 设置默认目录(找到DocumentRoot,默认的为# DocumentRoot "/var/www/html")
    DocumentRoot "/home/sum/" # 设定自己的目录

    同样Directory也要修改: <Directory "/var/www/html">修改为<Directory "/home/sum/">
    有些人可能不知道Directory 是哪一个Directory 
    # Further relax access to the default document root: 下面的Directory
    然后运行访问:
        假如我的/home/sum/upload/ 是个upload文件夹
        www.xxxxx.cn:8888/upload
    如果你一直访问
        www.xxxxx.cn:8888
    那么出来的一直是apache的主页

 

4、附加:文件上传大小设置、中文显示乱码设置、想put文件到httpd文件服务器设置 

 

# 文件上传大小设置(在/etc/httpd/conf/httpd.conf)也就是它的配置文件加入一行
LimitRequestBody 104857600

# 中文显示乱码(同上加入一行)
IndexOptions Charset=UTF-8

# 想上传文件到httpd 需要修改, 这样才可以上传
<Directory "/home/sum/">
    Dav On
    AllowOverride None
    # Options FollowSymLinks # 浏览器访问不显示文件列表/目录,隐藏文件列表
    Options All    # 在浏览器显示文件列表/目录
    Order allow,deny
    Allow from all
    Require all granted
    # 上面引用了mod_headers模块儿
    # 根据你们的需要,有的时候ajax访问图片会报跨域问题, 所以我习惯加上
    Header set Access-Control-Allow-Origin *
</Directory >

# 否则上传文件时:returned a response status of 405 Method Not Allowed
# 同时linux 对应的"/home/sum/"文件夹路径需要有写入权限


5、上传文件

import java.io.File;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

/**
 * 文件上传帮助类
 * @author yaco
 * @Date 2019年7月18日 下午2:16:09
 */
public class UploadUtils {

	/**
	 * 上传到文件服务器
	 * @author: yaco
	 * @Date  : 2019年7月18日 下午4:26:57  
	 */
	public static String uploadFile(byte[] file, String filePath, String fileName) throws Exception {
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		Client client = new Client();
		WebResource resource = client.resource(filePath + fileName);
		resource.put(String.class, file);
		return filePath + fileName;
	}
}

<!-- 文件传输 -->
<dependency>
	<groupId>com.sun.jersey</groupId>
	<artifactId>jersey-client</artifactId>
	<version>1.19.4</version>
</dependency>
<dependency>
	<groupId>org.glassfish.jersey.core</groupId>
	<artifactId>jersey-client</artifactId>
</dependency>

更新时间: 2020年5月3日06:23:21 新增Apache跨域问题(已解决)

更新时间:2020年7月1日20:38:35 隐藏文件服务器在浏览器访问可以查看到文件目录

 

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cocosum

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值